﻿//Retrieving data for the Profile tab
function populateProfile()
{
    if (!tab_profile_loaded)
        show_wait("profile_tab");
    osapi.http.get({
        'href': get_rest_people_self(current_owner_id),
        'format': 'json',
        'oauth_service_name': oauth_service_name,
        'authz': authz_type
    }).execute(fill_self_details);
}

//Filling the Profile tab with data
function fill_self_details(data)
{
    show_errors_if_any(data);

    //saving hash in order to know when new data arrives, 
    //if the hash is the same when the tab is refreshed - no need to redraw it
    if (compare_and_save_hash(data.metadata.DataHash))
        return;
    
    show_wait("profile_tab");

    var tab_container = $("profile_tab_container");
    remove_all_children(tab_container);

    // container for retrieved data
    var container = new Element('div');
    container.addClassName('contentbox');
    tab_container.appendChild(container);

    //greeting line
    var greeting = new Element('div');
    greeting.addClassName('greeting');
    greeting.update('Hello, ' + data.content.entry.displayName + '. You have logged in successfully. Wellcome back!');
    container.appendChild(greeting);

    //for later usage, data about the owner
    _owner_displayName = data.content.entry.displayName;
    _owner_profileUrl = data.content.entry.profileUrl;

    hide_wait("profile_tab");
    tab_profile_loaded = true;
}

function update_gadgets()
{
    $('update_image').setAttribute('src', update_image_moving_url);
    update_profile();
    $('update_image').setAttribute('src', update_image_url);
}